//
//  SHNerAreaList.m
//  JiFengDaRen
//
//  Created by SH_iOS on 2017/5/3.
//  Copyright © 2017年 SH_iOS. All rights reserved.
//
//static CGFloat const PickerViewHeight = 240;
static CGFloat const PickerViewLabelWeight = 32;
#import "SHNerAreaList.h"
#import "TDProvince.h"
@interface SHNerAreaList()<UIPickerViewDataSource, UIPickerViewDelegate>
@property(nonatomic,strong)UIPickerView *pickerView;
@property(nonatomic,strong)UIView *areaBgView;
@property(nonatomic,strong)UIButton *confirmBtn;
@property(nonatomic,strong)UIButton *cancelBtn;


@property(nonatomic,strong)NSArray *cityListArr;
/** 1.数据源数组 */
@property (nonatomic, strong, nullable)NSArray *arrayRoot;
/** 2.当前省数组 */
@property (nonatomic, strong, nullable)NSMutableArray *arrayProvince;
/** 3.当前城市数组 */
@property (nonatomic, strong, nullable)NSMutableArray *arrayCity;
/** 4.当前地区数组 */
//@property (nonatomic, strong, nullable)NSMutableArray *arrayArea;
/** 5.当前选中数组 */
@property (nonatomic, strong, nullable)NSArray *arraySelected;

/** 8.边线 */
@property (nonatomic, strong, nullable)UIView *lineView;

/** 9.省份 */
@property (nonatomic, strong, nullable)NSString *province;
/** 10.城市 */
@property (nonatomic, strong, nullable)NSString *city;

/** 9.省份ID */
@property (nonatomic, strong, nullable)NSString *provinceId;
/** 10.城市ID */
@property (nonatomic, strong, nullable)NSString *cityId;

/** 11.地区 */
@property (nonatomic, strong, nullable)NSString *area;
@property (nonatomic, assign)NSInteger proRow;
@property (nonatomic, assign)NSInteger cityRow;




@end
@implementation SHNerAreaList


- (NSMutableArray *)arrayProvince
{
    if (!_arrayProvince) {
        _arrayProvince = [NSMutableArray arrayWithCapacity:0];
    }
    return _arrayProvince;
}

- (NSMutableArray *)arrayCity
{
    if (!_arrayCity) {
        _arrayCity = [NSMutableArray arrayWithCapacity:0];
    }
    return _arrayCity;
}



- (instancetype)initWithFrame:(CGRect)frame{
    frame = CGRectMake(0, 0, KScreenWidth,KScreenHeight);
    if (self = [super initWithFrame:frame]) {
        [self iniData];
    }
    
    return self;
}

- (void)loadData
{
    if (self.cityListArr.count == 0) {
        return;
    }
    
    NSArray *citys  =   [[self.cityListArr firstObject] cityDataSourceArr];
    for (TDCity *city in citys) {
        [self.arrayCity addObject:city];
    }
    
    TDProvince *province = [self.cityListArr firstObject];
    self.province = province.provName;
    self.provinceId = province.provId;
    TDCity *city = [self.arrayCity firstObject];
    self.city = city.cityName ;
    self.cityId = city.cityId;

    [self areaBgView];
    [self confirmBtn];
    [self cancelBtn];
    [self pickerView];

}

- (void)iniData{

    if ([[NSUserDefaults standardUserDefaults]objectForKey:CITIYLIST]) {
        self.cityListArr =  [TDProvince userDefaultObjectWithArray:[[NSUserDefaults standardUserDefaults]objectForKey:CITIYLIST]];
         [self loadData];

    }else{
        __weak typeof(self) weakSelf = self;
        [self getDataArrayComplete:^(NSArray *array) {
            weakSelf.cityListArr = array;
            [self loadData];
        }];
        
    }

}

- (void)getDataArrayComplete:(void(^)( NSArray *array))complete{
    
    [MyDataService requestNoRSAAFURL:KGET_PROVINCE_AND_CITY httpMethod:@"GET" params:nil data:nil complection:^(id result) {
        
        BOOL success = [result[@"success"] boolValue];
        if (success) {
            NSArray *dataArray = [result objectForKey:@"data"];
            
            [[NSUserDefaults standardUserDefaults] setObject:dataArray forKey:CITIYLIST];
            [[NSUserDefaults standardUserDefaults] synchronize];
            complete([TDProvince userDefaultObjectWithArray:dataArray]);
        
        }
        
    } failure:^(id erro) {
        self.alpha = 0;
        [self removeFromSuperview];

    }];

}
- (UIView *)areaBgView{
    
    if (!_areaBgView) {
        _areaBgView = [[UIView alloc]initWithFrame:CGRectMake(0,KScreenHeight, KScreenWidth, __I6Height(240))];
        _areaBgView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_areaBgView];
    }
    return _areaBgView;
}
- (UIPickerView *)pickerView
{
    if (!_pickerView) {
    
        _pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0,__I6Height(40), KScreenWidth, __I6Height(200))];
        [_pickerView setBackgroundColor:[UIColor whiteColor]];
        [_pickerView setDataSource:self];
        [_pickerView setDelegate:self];
        [self.areaBgView addSubview:_pickerView];
       
        
    }
    return _pickerView;
}

- (UIButton *)confirmBtn{
    if (!_confirmBtn) {
        _confirmBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth - __I6Width(60), 0, __I6Width(60), __I6Height(40))];
        [_confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
        _confirmBtn.titleLabel.font  = KSystemFont(__I6Width(16));
        [_confirmBtn setTitleColor:KTEXTCOLOR forState:UIControlStateNormal];
        [_confirmBtn addTarget:self action:@selector(confirmBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.areaBgView addSubview:_confirmBtn];
    }
    return _confirmBtn;
}

- (UIButton *)cancelBtn{
    if (!_cancelBtn) {
        _cancelBtn = [[UIButton alloc]initWithFrame:CGRectMake(__I6Width(5), 0, __I6Width(60), __I6Height(40))];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        _cancelBtn.titleLabel.font  = KSystemFont(__I6Width(16));
        [_cancelBtn setTitleColor:KTEXTCOLOR forState:UIControlStateNormal];
        [_cancelBtn addTarget:self action:@selector(cancelBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.areaBgView addSubview:_cancelBtn];
    }
    return _cancelBtn;
}
#pragma mark - --- delegate 视图委托 ---

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) {
        return self.cityListArr.count;
    }else {
        return self.arrayCity.count;
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return PickerViewLabelWeight;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0) {
        self.arraySelected = [self.cityListArr[row] cityDataSourceArr];
        [self.arrayCity removeAllObjects];
        for (TDCity *city in self.arraySelected) {
            [self.arrayCity addObject:city];
        }
    
        [pickerView reloadComponent:1];
        [pickerView selectRow:0 inComponent:1 animated:YES];

    }else if (component == 1) {
        if (self.arraySelected.count == 0) {
            self.arraySelected = [[self.cityListArr firstObject] cityDataSourceArr];
        }
        
    }else{
    }
    
    [self reloadData];


}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view{
    
    NSString *text;
    if (component == 0) {
        text =  [self.cityListArr[row] provName];

    }else if (component == 1){
        text =  [self.arrayCity[row] cityName];
    }else{
   
    }
    
    
    UILabel *label = [[UILabel alloc]init];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label setFont:[UIFont systemFontOfSize:__I6Width(17)]];
    [label setText:text];

    return label;
    
    
}


- (void)reloadData
{
    if (self.cityListArr.count == 0) {
        return;
    }
    NSInteger index0 = [self.pickerView selectedRowInComponent:0];
    NSInteger index1 = [self.pickerView selectedRowInComponent:1];

    self.province = [self.cityListArr[index0] provName];
    self.provinceId = [self.cityListArr[index0] provId];
    [self.arrayCity removeAllObjects];
    for (TDCity *city in [self.cityListArr[index0] cityDataSourceArr]) {
        [self.arrayCity addObject:city];
    }
    self.city = [self.arrayCity[index1] cityName];
    self.cityId = [self.arrayCity[index1] cityId];
    
}


- (void)show{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.1];

    CGRect bgFrame = self.areaBgView.frame;
    bgFrame.origin.y = KScreenHeight - __I6Height(240);
    [UIView animateWithDuration:.3 animations:^{
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];

        self.areaBgView.frame = bgFrame;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)confirmBtnAction:(UIButton *)btn{
   
    CGRect bgFrame = self.areaBgView.frame;
    bgFrame.origin.y = KScreenHeight;
    [UIView animateWithDuration:.3 animations:^{
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.01];
        self.areaBgView.frame = bgFrame;
    } completion:^(BOOL finished) {

        if (_delegate &&[ _delegate respondsToSelector:@selector(pickerArea:province:city:provinceId:cityId:area:)]) {
            [_delegate pickerArea:self province:self.province city:self.city provinceId:self.provinceId cityId:self.cityId area:@""];
        }
        [self removeFromSuperview];
        
    }];

}

- (void)cancelBtnAction:(UIButton *)btn{
    
    CGRect bgFrame = self.areaBgView.frame;
    bgFrame.origin.y = KScreenHeight;
    [UIView animateWithDuration:.3 animations:^{
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.01];
        self.areaBgView.frame = bgFrame;
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
        
    }];

}

#pragma mark - 检测pickerView 是否正在滚动
- (BOOL)anySubViewScrolling:(UIView *)view{
    if ([view isKindOfClass:[UIScrollView class]]) {
        UIScrollView *scrollView = (UIScrollView *)view;
        if (scrollView.dragging || scrollView.decelerating) {
            return NO;
        }
    }
    for (UIView *theSubView in view.subviews) {
        if ([self anySubViewScrolling:theSubView]) {
            return NO;
        }
    }
    return YES;
}
@end
